<?php

namespace app\admin\controller\org;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use think\Exception;
/**
 * 组织结构类别
 *
 * @icon fa fa-circle-o
 */
class Orgtype extends Backend
{
    protected $model = null;
    protected $searchFields = 'name|linkman';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\org\Orgtype;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $is_super = Db::name('admin')->where('id', AID)->value('is_super');
        $this->assignconfig("is_super", $is_super);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index(){
        //当前是否为关联查询
        $this->relationSearch = false;
        if($this->request->isAjax()){
            if($this->request->request('keyField')){
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->where('parent_id', 0)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where('parent_id', 0)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key => $row) {
                $row->sort =  ($key+1+$offset);
                $row->mobile = substr_replace($row->mobile, '****', 3, 4);
            }
            $list = collection($list)->toArray();
            $result = array('total'=>$total, 'rows'=>$list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row= $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                if (!$params['mobile'] || !\think\Validate::regex($params['mobile'], "^1\d{10}$")) {
                    $this->error(__('手机号不正确'));
                }
                if($params['type'] == 2){//付费单位
                    if(empty($params['start_time']) || empty($params['end_time'])){
                        $this->error(__('付费单位需要有起始日期！'));
                    }
                    $params['start_time'] = strtotime($params['start_time'] );
                    $params['end_time'] = strtotime($params['end_time'] );
                    if($params['start_time'] >=$params['end_time'] ){
                        $this->error(__('结束时间必须大于开始时间！'));
                    }
                }
                Db::startTrans();
                try{
                    $result = $row->save($params);
                    if ($result === false) {
                        $this->error($row->getError());
                    }
                    Db::name('admin')->where('org_cate_id', $ids)->where('is_super',2)->update(['mobile'=>$params['mobile'],'username'=>$params['linkman']]);
                    Db::commit();
                }catch (Exception $e){
                    Db::rollback();
                    $this->error($e->getMessage());
                }

                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
